package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.mapper.BaseDictMapper;
import com.igeekhome.pojo.BaseDict;
import com.igeekhome.pojo.BaseDictExample;
import com.igeekhome.service.IDiceService;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Romeo
 */
@Service("diceService")
public class DiceServiceImpl implements IDiceService {

    @Autowired
    private BaseDictMapper baseDictMapper;


    @Override
    public PageInfo lists(Integer page, Integer limit, String code) {
        PageHelper.startPage(page,limit);
        BaseDictExample example = new BaseDictExample();
        example.createCriteria().andDictTypeCodeEqualTo(code);
        List<BaseDict> dictList = baseDictMapper.selectByExample(example);

        PageInfo<BaseDict> pageInfo = new PageInfo<>(dictList);

        return pageInfo;
    }

    @Override
    public List<BaseDict> list(String code) {
        BaseDictExample example = new BaseDictExample();
        example.createCriteria().andDictTypeCodeEqualTo(code);
        List<BaseDict> dictList = baseDictMapper.selectByExample(example);
        return dictList;
    }

    /**
     *
     * @param baseDict 传入字典的所有数据（在controller中设置问题件状态码，默认状态为1开启状态）
     * @return
     */
    @Override
    public ResultBean add(BaseDict baseDict) {
        int insert = baseDictMapper.insert(baseDict);
        if(insert > 0){
            return ResultBean.ok();
        }
        return ResultBean.fail("新增失败");
    }

    @Override
    public ResultBean edit(BaseDict baseDict) {
        int update = baseDictMapper.updateByPrimaryKeySelective(baseDict);
        if(update > 0){
            return ResultBean.ok();
        }
        return ResultBean.fail("新增失败");
    }

    @Override
    public ResultBean delete(String idStr) {
        int delete = 0;
        if(CommonUtil.isEmpty(idStr)){
            return ResultBean.fail("参数不全");
        }
        if (idStr.contains(",")) {
            delete = baseDictMapper.deleteByIdStr("( "+idStr+" )");
        }else {
            delete = baseDictMapper.deleteByPrimaryKey(Integer.parseInt(idStr));
        }

        if(delete > 0){
            return ResultBean.build(0,"删除成功",null);
        }
        return ResultBean.fail("删除失败");
    }

    @Override
    public TableResultBean list(Integer page, Integer limit, String code) {
        BaseDictExample example = new BaseDictExample();
        example.createCriteria().andDictTypeCodeEqualTo(code);
        List<BaseDict> problemType = baseDictMapper.selectByExample(example);
        PageInfo pageInfo = lists(page, limit, code);
        if(problemType.size() > 0 && problemType != null){
            return TableResultBean.ok(pageInfo.getTotal(),problemType);
        }
        return TableResultBean.fail("暂无数据");
    }

    @Override
    public List<BaseDict> selectByTypeCode(String dictTypeCode) {
        BaseDictExample example = new BaseDictExample();
        example.createCriteria().andDictTypeCodeEqualTo(dictTypeCode).andDictEnableEqualTo("1");
        return baseDictMapper.selectByExample(example);
    }
}
